<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/5/27
 * Time: 14:17
 * Mail:87658989@qq.com
 */

namespace app\base\filter;


class Common
{
    public $field;
    public $title;
    public $fieldArr;
    public $fieldTitle;
    public $fieldNote = [];

    public $note = "查询条件json输入格式如{\'order_state\':[\'in\',\'6,7,8,9,10,11,12\'],\'passenger_name\':[\'like\',\'%小明%\']}";

    protected  $p_fields; // 外部传入的字段参数
    protected  $p_where; // 外部传入的条件参数
    protected  $p_filtered = false; // 是否已经过滤

    public $other = []; // 不参与查询的自定义字段

    public function __construct($fields = null, $where = null){
        $this->p_fields = $fields;
        $this->p_where = $where;
    }

    /**
     * 根据实例化传入的参数进行过滤
     * @return array
     */
    public function geFiltered(){
        $this->analyzeField();
        list(,$field) = $this->checkField($this->p_fields);
        list(,$where) = $this->checkWhere($this->p_where);

        // 标记已过滤
        $this->p_filtered = true;

        return ['field' => $field, 'where' => $where , 'note'=>$this->fieldNote];
    }

    /**
     * 返回该接口的备注，字段说明等
     * @return array
     */
    public function getMark(){
        !$this->p_filtered && ( $this->geFiltered() );

        $data = [];
        $data['note'] = $this->note;
        $data['description'] = $this->title;
        $data['fields'] = $this->fieldTitle;

        // 将$other中定义的字段填入说明中
        foreach($this->other as $key => $item){
            $arr = explode('|', $item);
            $desc = [
                'name' => isset($arr) && isset($arr[0]) ? $arr[0] : $key,
                'field' => $key,
                'allow_search' => 0,
                'type' => isset($arr) && isset($arr[2]) ? $arr[2] : '',
            ];

            $data['description'][$key] = $desc;
            $data['fields'][$key] = $desc['name'];
        }

        return $data;
    }

    /**
     * 解析顾虑器字段
     * @return $this
     */
    public function analyzeField(){
        foreach ($this->field as $k=>$v){
            $arr              = explode('|',$v);
            $this->fieldArr[] = isset($arr[1]) && !empty($arr[1]) ? $k.' as '.$arr[1] : $k;
            if(strstr($k,'.')){
                $key = isset($arr[1]) && !empty($arr[1]) ? $arr[1] : substr($k,strpos($k,'.')+1,strlen($k));
            }else{
                $key = isset($arr[1]) && !empty($arr[1]) ? $arr[1] : $k;
            }
            $this->title[$key]['name']  = isset($arr[0]) && !empty($arr[0])  ? $arr[0] : $k;
            $this->title[$key]['field'] = $k;
            if(isset($arr[3]) && !empty($arr[3])){
                $this->title[$key]['allow_search'] = $arr[3];
                $this->title[$key]['type']         = $arr[2];
            }
        }
        return $this;
    }

    /**
     * 检查查询条件合法性 条件1|条件2 => []
     * @param array $where
     * @return array
     */
    public function checkWhere($where=[]){
        if($where==[]){
            return [1,[]];
        }
        $newField = [];
        $pass     = true;
        $msg      = '';
        foreach ($where as $key=>$value){
            $mixKey = explode('|',$key);
            if(count($mixKey)>1){
                foreach ($mixKey as $mk){
                    if(isset($this->title[$mk])){
                        $field = $this->title[$mk]['field'];
                        $tempField[] = $field;
                    }else{
                        $pass = false;
                        $msg .= $mk.'查询条件不存在';
                    }
                }
                $newField[implode('|',$tempField)] = $value;
                continue;
            }
            if(isset($this->title[$key])){
                $field = $this->title[$key]['field'];
                $newField[$field] = $value;
            }else{
                $pass = false;
                $msg .= $key.'查询条件不存在';
            }
        }
        if($pass){
            return [1,$newField];
        }else{
            abort(0,$msg);
        }
    }

    /**
     * 检查输入字段合法性,生成新title
     * @param string $field
     * @return array
     */
    public function checkField($field='*'){
        if(!$field) $field = '*';
        if($field=='*'){
            foreach ($this->title as $key=>$value){
                $this->fieldTitle[$key] = $value['name'];
            }
            return [1,implode(',',$this->fieldArr)];
        }
        $inputField = explode(',',$field);
        $newField   = [];
        $pass       = true;
        $msg        = '';
        foreach ($inputField as $value){
            if(isset($this->title[$value])){
                $tableField = $this->title[$value]['field'];
                $newField[] = strstr($tableField,$value) ? $tableField : $tableField.' as '.$value;
                $this->fieldTitle[$value] = $this->title[$value]['name'];
            }else{
                $pass = false;
                $msg .= $value.'不在字段范围内';
            }
        }
        if($pass){
            return [1,implode(',',$newField)];
        }else{
            abort(0,$msg);
        }
    }

    /**
     * 过滤器数据处理
     * @param array $data
     * @return array
     */
    public function checkData($data=[]){
        return $data;
    }
}